﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：增自来水表
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.YongShui.YongHuGL
{
    public partial class ZengZiLSSB : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private string SHUIBIAOBH
        {
            get
            {
                return Convert.ToString(ViewState["_SHUIBIAOXXID"]);
            }
            set
            {
                ViewState["_SHUIBIAOXXID"] = value;
            }
        }

        /// <summary>
        /// 当前操作用户的用户号
        /// </summary>
        private string YONGHUH
        {
            get
            {
                return Convert.ToString(ViewState["_YONGHUH"]);
            }
            set
            {
                ViewState["_YONGHUH"] = value;
            }
        }

        /// <summary>
        /// 当前操作用户站点
        /// </summary>
        private string ZHANDIANBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZHANDIANBH"]);
            }
            set
            {
                ViewState["_ZHANDIANBH"] = value;
            }
        }

        /// <summary>
        /// 记录当前查询的水表号,保存在dictionary中,方便实现查询内容记忆功能
        /// </summary>
        private Dictionary<string, string> shuiBiaoBHs
        {
            get
            {
                if (ViewState["shuiBiaoBHs"] == null)
                {
                    ViewState["shuiBiaoBHs"] = new Dictionary<string, string>();

                }
                return ViewState["shuiBiaoBHs"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 记录本次增表的水表号,保存在dictionary中,方便实现本次增表错误可进行即时删除
        /// </summary>
        private Dictionary<string, string> zbshuiBiaoBHs
        {
            get
            {
                if (ViewState["zbshuiBiaoBHs"] == null)
                {
                    ViewState["zbshuiBiaoBHs"] = new Dictionary<string, string>();

                }
                return ViewState["zbshuiBiaoBHs"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 记录列表二查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL _YS_CB_SHUIBIAOXXBLL = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 自来水公司数据BLL
        /// </summary>
        YS_CB_ZILAISGSSJBBLL _YS_CB_ZILAISGSSJBBLL = new YS_CB_ZILAISGSSJBBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                //获取URL参数用户号
                this.YONGHUH = this.Request.QueryString["yonghuh"];
                InitData();
            }
            this.gvList1.DataSource = this.dtSource;
            this.gvList1.DataBind();
            SetDeleteVisible();
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        /// <summary>
        /// 查询水表
        /// </summary>
        protected void btnSearch2_Click(object sender, EventArgs e)
        {
            LoadData2(true);
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            this.SHUIBIAOBH = e.CommandArgument.ToString();
            //清除该水表对应的编号记录
            this.shuiBiaoBHs.Remove(this.SHUIBIAOBH);
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource2.Rows.Count; i++)
            {
                if (this.dtSource2.Rows[i]["S_SHUIBIAOBH"].ToString() == this.SHUIBIAOBH)
                {
                    this.dtSource2.Rows.RemoveAt(i);
                }
            }
            //重新绑定数据源
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.txtYongHuM.Text == "")
            {
                PageTools.AlertMsg(this,"请先查询出要增表的用户");
                return;
            }
            //如果无数据,返回
            if (this.dtSource2==null)
            {
                return;
            }
            //判断用户号是否正确
            DataRowCollection drc = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and I_LEIXING=1 and S_YONGHUH='{0}'", this.txtYongHuH.Text), null).Rows;
            if (drc.Count != 1)
            {
                this.YONGHUH = "-9999";
                PageTools.AlertMsg(this, "要增表的用户号不存在");
                this.txtYongHuH.Focus();
                return;
            }
            for (int i = 0; i < this.gvList2.VisibleRowCount; i++)
            {
                //执行增表操作
                YS_CB_YONGHUSBGX gx = new YS_CB_YONGHUSBGX
                {
                    I_JLZT = 0,
                    I_LEIXING = 1,
                    S_ZHANDIANBH = this.ZHANDIANBH,
                    S_YONGHUBH = this.YONGHUH
                };
                this.SHUIBIAOBH = this.gvList2.GetRowValues(i, "S_SHUIBIAOBH").ToString();
                gx.S_SHUIBIAOBH = this.SHUIBIAOBH;
                gx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                gx.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                //执行增表
                _spHelper.P_YS_CB_ADDSHUIBIAO(this.ZHANDIANBH, this.YONGHUH, gx.S_SHUIBIAOBH, gx.S_CAOZUOR, gx.D_CAOZUOSJ);
                //记录日志
                _SYS_LOGBLL.Log<YS_CB_YONGHUSBGX>(LoginInfo.CurrentUser,BusinessTool.WType.ToString(), "增自来水表", OperateState.Add, gx, null);
                //记录所增水表
                this.zbshuiBiaoBHs.Add(this.SHUIBIAOBH, this.SHUIBIAOBH);
            }
            //增表成功 清空列表2
            //清除该水表对应的编号记录
            this.shuiBiaoBHs.Clear();
            this.dtSource2.Rows.Clear();
            //重新绑定数据源
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
            //重新绑定用户下水表
            LoadData(false);
        }

        /// <summary>
        /// 对当前新增的表进行删除
        /// </summary>
        protected void lnbDel_Command1(object sender, CommandEventArgs e)
        {
            this.SHUIBIAOBH = e.CommandArgument.ToString();
            //将新加的水表删除
            _spHelper.P_YSCB_XIAOBIAOJS(this.txtYongHuH.Text, this.SHUIBIAOBH);
            //清除该水表对应的编号记录
            this.zbshuiBiaoBHs.Remove(this.SHUIBIAOBH);
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (this.dtSource.Rows[i]["S_SHUIBIAOBH"].ToString() == this.SHUIBIAOBH)
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            //重新绑定数据源
            this.gvList1.DataSource = this.dtSource;
            this.gvList1.DataBind();
            SetDeleteVisible();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            if (this.YONGHUH != "")
            {
                //加载用户信息及水表
                this.txtYongHuH.Text = this.YONGHUH;
                LoadData(false);
                //禁用用户区域文本框及查询按钮
                this.txtYongHuH.Enabled = false;
                this.btnSearch.Enabled = false;
            }
            else
            {
                this.txtYongHuH.Enabled = true;
                this.btnSearch.Enabled = true;
            }
            this.txtYongHuM.Enabled = false;
            this.txtDiZhi.Enabled = false;
        }

        /// <summary>
        /// 查询用户下水表
        /// </summary>
        private void LoadData(bool alert)
        {
            lblErr.Text = "";
            if (!string.IsNullOrEmpty(this.txtYongHuH.Text))
            {
                DataRowCollection drc = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and S_Yonghuh='{0}' and i_leixing=1", this.txtYongHuH.Text, BusinessTool.ZHANDIANBH() == "00" ? "" : " and S_ZHANDIANBH='" + BusinessTool.ZHANDIANBH() + "'"), null).Rows;
                if (drc.Count == 1)
                {
                    //记忆当前用户号
                    this.YONGHUH = (drc[0]["S_YONGHUH"]).ToString();
                    this.txtYongHuM.Text = drc[0]["S_YONGHUM"].ToString();
                    this.txtDiZhi.Text = drc[0]["S_DIZHI"].ToString();
                    this.ZHANDIANBH = drc[0]["S_ZHANDIANBH"].ToString();
                    //加载其下水表
                    string sWhere = "'------'";
                    foreach (DataRow dr in _YS_CB_YONGHUSBGXBLL.SelectYS_CB_YONGHUSBGXesDynamic(string.Format("I_JLZT=0 and I_LEIXING=1 and S_YONGHUBH='{0}' and I_SHUIBIAOZT=0", this.txtYongHuH.Text), null).Rows)
                    {
                        sWhere += ",'" + dr["S_SHUIBIAOBH"].ToString() + "'";
                    }
                    this.dtSource = _YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXXesDynamic(String.Format("I_LEIXING=1 and S_SHUIBIAOBH in ({0})", sWhere), null);
                    this.gvList1.DataSource = this.dtSource;
                    this.gvList1.DataBind();
                    SetDeleteVisible();
                    //切换用户时,下方查询的水表号清空
                    this.shuiBiaoBHs.Clear();
                    this.dtSource2=null;
                    this.gvList2.DataSource = this.dtSource2;
                    this.gvList2.DataBind();
                }
                else
                {
                    if (alert)
                    {
                        PageTools.AlertMsg(this, "该用户不存在,请重新输入！");
                    }
                }
            }
        }

        /// <summary>
        /// 根据输入的水表号查询水表
        /// </summary>
        private void LoadData2(bool alert)
        {
            //判断当前有无用户
            if (this.txtYongHuM.Text=="")
            {
                PageTools.AlertMsg(this,"请先查询出用户！");
                return;
            }
            //计算要查询的水表ID
            string bhs = "";
            foreach (var item in this.shuiBiaoBHs.Keys)
            {
                bhs = bhs + item + ",";
            }
            if (this.txtConShuiBiaoH.Text != "")
            {
                //查询该水表对应情况,判断是否可用
                DataTable dtTemp = _spHelper.P_YS_CB_PANDUANSHZBKY(this.txtConShuiBiaoH.Text.Trim(),this.txtYongHuH.Text.Substring(0,2),this.txtYongHuM.Text).Tables[0];
                if (Convert.ToString(dtTemp.Rows[0][0]) == "-1")
                {
                    this.dtSource2 = null;
                    this.gvList2.DataSource = null;
                    this.gvList2.DataBind();
                    string str1 = dtTemp.Rows[0][1].ToString();
                    PageTools.AlertMsg(this, str1);
                    this.txtConShuiBiaoH.Focus();
                    return;
                }
                //判断查询的水表号是否已经存在记忆列表中,不存在则添加
                if (!this.shuiBiaoBHs.ContainsKey(this.txtConShuiBiaoH.Text))
                {
                    if (Convert.ToString(dtTemp.Rows[0][0]) == "-2")
                        lblErr.Text += dtTemp.Rows[0][1].ToString() + "\n<br>";
                    this.shuiBiaoBHs.Add(this.txtConShuiBiaoH.Text, this.txtConShuiBiaoH.Text);
                    bhs = bhs + this.txtConShuiBiaoH.Text + ",";
                }
            }
            if (bhs == "")
            {
                return;
            }
            else
            {
                bhs = "," + bhs;
            }
            //查询
            DataTable dt = this._YS_CB_ZILAISGSSJBBLL.SelectYS_CB_ZILAISGSSJsDynamic(string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)>0", bhs), string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)", bhs));
            this.dtSource2 = dt;
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
            //定位刚查询的数据
            if (this.dtSource2.Rows.Count > 0)
            {
                PageTools.FocusOpreateRecord(this.dtSource2, this.gvList2, "S_SHUIBIAOBH", this.txtConShuiBiaoH.Text, true);
            }
        }

        /// <summary>
        /// 控制删除是否显示
        /// </summary>
        private void SetDeleteVisible()
        {
            //根据是否是本页面新增表显示删除
            for (int i = 0; i < this.gvList1.VisibleRowCount; i++)
            {
                DataRowView drv = (DataRowView)this.gvList1.GetRow(i);
                LinkButton cb = (LinkButton)gvList1.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList1.Columns[3], "lnbDel");
                if (this.zbshuiBiaoBHs.ContainsKey(Convert.ToString(drv.Row["S_SHUIBIAOBH"])))
                {
                    cb.Visible = true;
                }
                else
                {
                    cb.Visible = false;
                }
            }
        }

        #endregion
        
    }
}